/// Holds and shares the data cross entire app.
/// All data store in a [Map<String, dynamic>] and manipulated by key.
class DataSharing {
  static final Map<String, dynamic> _sharedData = {};

  /// Gets the data with [key].
  static T? get<T>(String key) {
    return _sharedData[key] as T?;
  }

  /// Sets the data with [key] and [value].
  static void set<T>(String key, T? value) {
    _sharedData[key] = value;
  }

  /// Removes the data. [key] used to remove single item. [keys] used to remove multiple items.
  /// If both provided the [keys] takes priority.
  static void remove({List<String>? keys, String? key}) {
    assert(keys != null || key != null);

    keys ??= [key!];

    for (final key in keys) {
      _sharedData.remove(key);
    }
  }

  /// Removes all data.
  static void removeAll() {
    while (_sharedData.keys.isNotEmpty) {
      remove(keys: _sharedData.keys.toList());
    }
  }
}
